package cn.mado.org.controller;

import cn.mado.basic.note.SysLog;
import cn.mado.basic.util.AjaxResult;
import cn.mado.basic.util.PageList;
import cn.mado.org.domain.Writer;
import cn.mado.org.query.WriterQuery;
import cn.mado.org.service.WriterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/writer")
public class WriterController {
    @Autowired
    private WriterService writerService;
    //添加修改
    @SysLog("添加修改一位作家")
    @PostMapping("/save")
    public AjaxResult saveWriter(@RequestBody Writer writer){
        if (null==writer.getWriterId()){
            writerService.addWriter(writer);
        }else {
            writerService.updateWriter(writer);
        }
        return AjaxResult.me().success();
    }

    //删除
    @SysLog("删除一位作家")
    @DeleteMapping("/{writerId}")
    public AjaxResult deleteWriter(@PathVariable("writerId") Long writerId){
        writerService.deleteWriterById(writerId);
        return AjaxResult.me().success();
    }

    //查询全部
    @GetMapping
    public List<Writer> findAllWriter(){
        return writerService.findAll();
    }

    //查询单条
    @SysLog("id查询一位作家")
    @GetMapping("/{writerId}")
    public Writer findAllWriter(@PathVariable("writerId") Long writerId){
        return writerService.findWriterById(writerId);
    }

    //分页
    @PostMapping
    public PageList<Writer> queryByPage(@RequestBody WriterQuery writerQuery){
        return writerService.queryByPage(writerQuery);
    }
}
